package com.riant.aop;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class TestMd5 {
    public static final String MD5="MD5";
    public static void main(String[] args) {
        test1("hello,world");
        test2("hello,world");
    }

    public static void test1(String message){
        try {
            MessageDigest digest = MessageDigest.getInstance(MD5);
            byte[] originBytes=message.getBytes();
            byte[] resultByte = digest.digest(originBytes);
            System.out.println(resultByte.length);
            //一个字符占据16位，2个字节，在这里字节数组转成字符串时，实际是变成字符数组，字符数组长度为字节数组的两倍，
            String result = Hex.encodeHexString(resultByte);
            System.out.println("md5 result:"+result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    //使用DigestUtils工具类中算法
    public static String test2(String message){
        String md5Hex = DigestUtils.md5Hex(message.getBytes());
        System.out.println("md5Hex: "+md5Hex);
        return md5Hex;
    }


}
